<?php

namespace App\Services\Core\Sms\Dahan;


use App\Helpers\Logger\SLogger;
use App\Models\Factory\CacheFactory;
use App\Services\Core\Sms\SmsService;
use Log;


/**
 * 大汉短信通道
 * Class DahanService
 * @package App\Services\Core\Sms\Changzhuo
 */
class DahanService extends SmsService
{


    const accountSid = '300dd0f477354082ae95502e73e34ae9';
    const authToken 	= '9d7ded561ee746ac8c9d04259264f0fa';
    const apiUrl 	= 'https://sms.dahancloud.com/';
    const appId 		= 'a496a2e2e15146b39b0c06f9e37cb46e';

    /**
     * @param $mobile
     * @return string
     */
    public static  function send($mobile)
    {

        if (preg_match('/^0?(13|14|15|17|18)[0-9]{9}$/', $mobile)) {
        // 初始化SDK
        $sdk = new SmsSDK();
        $sdk->setAccountSid(self::accountSid);
        $sdk->setAuthToken(self::authToken);
        $sdk->setApiUrl(self::apiUrl);
        $sdk->setAppId(self::appId);
        $code = rand(1000, 9999);
        $datas[] = $code;
        $result = $sdk->sendSMS($mobile, '337', $datas);
            SLogger::getStream()->info('这是大汉短信通道!');
            CacheFactory::putValueToCache($mobile . '_code_kk', $code, 5 * 60);
            CacheFactory::putValueToCache($mobile . '_send_limit_kk', $mobile, 60);

            return json_encode(array( "msg" => "短信验证码发送成功", "check" => $code));
        } else {
            return json_encode(array( "error" => "44000227", "msg" => "短信验证码发送失败"));
        }
    }


//    private static function curlPost($url, $postFields)
//    {
//        $ch = curl_init();
//        curl_setopt($ch, CURLOPT_POST, 1);
//        curl_setopt($ch, CURLOPT_HEADER, 0);
//        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//        curl_setopt($ch, CURLOPT_URL, $url);
//        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
//        $result = curl_exec($ch);
//        curl_close($ch);
//        return $result;
//    }
}
